package com.podium.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author 邵文泽
 * @description 线程池配置
 * @date 2025/5/12 9:59
 */
@Configuration
@EnableAsync
public class AsyncConfig {

    private static final String LOG_THREAD_NAME_PREFIX = "Async-Log-Save-";

    /**
     * 日志线程池配置
     *
     * @return 线程池
     */
    @Bean(name = "logTaskExecutor")
    public ThreadPoolTaskExecutor logTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程池大小
        executor.setCorePoolSize(2);
        // 最大线程数
        executor.setMaxPoolSize(10);
        // 队列容量
        executor.setQueueCapacity(100);
        // 线程名前缀，便于调试和日志追踪
        executor.setThreadNamePrefix(LOG_THREAD_NAME_PREFIX);
        // 线程池关闭时等待所有任务都完成再继续销毁其他的Bean
        executor.setWaitForTasksToCompleteOnShutdown(true);
        // 设置最大等待时间，避免阻塞过久
        executor.setAwaitTerminationSeconds(60);
        executor.initialize();
        return executor;
    }

}
